{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "### 练习\n",
    "请使用迭代查找一个list中最小和最大值，并返回一个tuple："
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "def findMinAndMax(L):\n",
    "    while L == []:\n",
    "        return (None, None)\n",
    "\n",
    "    max = L[0]\n",
    "\n",
    "    min = L[0]\n",
    "\n",
    "    for num in L:\n",
    "\n",
    "        if num > max:\n",
    "            max = num\n",
    "\n",
    "        if num < min:\n",
    "            min = num\n",
    "\n",
    "    return min, max\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试成功!\n"
     ]
    }
   ],
   "source": [
    "# 测试\n",
    "if findMinAndMax([]) != (None, None):\n",
    "    print('测试失败!')\n",
    "elif findMinAndMax([7]) != (7, 7):\n",
    "    print('测试失败!')\n",
    "elif findMinAndMax([7, 1]) != (1, 7):\n",
    "    print('测试失败!')\n",
    "elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):\n",
    "    print('测试失败!')\n",
    "else:\n",
    "    print('测试成功!')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [
     "如果给定一个`list`或`tuple`，我们可以通过`for`循环来遍历这个`list`或`tuple`，这种遍历我们称为迭代（Iteration）。\n",
     "\n",
     "在Python中，迭代是通过`for ... in`来完成的，而很多语言比如C语言，迭代`list`是通过下标完成的，比如C代码：\n",
     "\n",
     "```\n",
     "for (i=0; i<length; i++) {\n",
     "    n = list[i];\n",
     "}\n",
     "```\n",
     "\n",
     "可以看出，Python的`for`循环抽象程度要高于C的`for`循环，因为Python的`for`循环不仅可以用在`list`或`tuple`上，还可以作用在其他可迭代对象上。\n",
     "\n",
     "`list`这种数据类型虽然有下标，但很多其他数据类型是没有下标的，但是，只要是可迭代对象，无论有无下标，都可以迭代，比如`dict`就可以迭代：\n",
     "\n",
     "```\n",
     ">>> d = {'a': 1, 'b': 2, 'c': 3}\n",
     ">>> for key in d:\n",
     "...     print(key)\n",
     "...\n",
     "a\n",
     "c\n",
     "b\n",
     "```\n",
     "\n",
     "因为`dict`的存储不是按照`list`的方式顺序排列，所以，迭代出的结果顺序很可能不一样。\n",
     "\n",
     "默认情况下，`dict`迭代的是key。如果要迭代value，可以用`for value in d.values()`，如果要同时迭代key和value，可以用`for k, v in d.items()`。\n",
     "\n",
     "由于字符串也是可迭代对象，因此，也可以作用于`for`循环：\n",
     "\n",
     "```\n",
     ">>> for ch in 'ABC':\n",
     "...     print(ch)\n",
     "...\n",
     "A\n",
     "B\n",
     "C\n",
     "```\n",
     "\n",
     "所以，当我们使用`for`循环时，只要作用于一个可迭代对象，`for`循环就可以正常运行，而我们不太关心该对象究竟是`list`还是其他数据类型。\n",
     "\n",
     "那么，如何判断一个对象是可迭代对象呢？方法是通过`collections.abc`模块的`Iterable`类型判断：\n",
     "\n",
     "```\n",
     ">>> from collections.abc import Iterable\n",
     ">>> isinstance('abc', Iterable) # str是否可迭代\n",
     "True\n",
     ">>> isinstance([1,2,3], Iterable) # list是否可迭代\n",
     "True\n",
     ">>> isinstance(123, Iterable) # 整数是否可迭代\n",
     "False\n",
     "```\n",
     "\n",
     "最后一个小问题，如果要对`list`实现类似Java那样的下标循环怎么办？Python内置的`enumerate`函数可以把一个`list`变成索引-元素对，这样就可以在`for`循环中同时迭代索引和元素本身：\n",
     "\n",
     "```\n",
     ">>> for i, value in enumerate(['A', 'B', 'C']):\n",
     "...     print(i, value)\n",
     "...\n",
     "0 A\n",
     "1 B\n",
     "2 C\n",
     "```\n",
     "\n",
     "上面的`for`循环里，同时引用了两个变量，在Python里是很常见的，比如下面的代码：\n",
     "\n",
     "```\n",
     ">>> for x, y in [(1, 1), (2, 4), (3, 9)]:\n",
     "...     print(x, y)\n",
     "...\n",
     "1 1\n",
     "2 4\n",
     "3 9\n",
     "```"
    ],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}